<div class="row-content am-cf">
    <div class="row">
        <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
            <div class="widget am-cf">
                <form id="my-form" class="am-form tpl-form-line-form" method="post">
                    <input type="hidden" name="s" value="">
                    <input type="hidden" name="step" value="2">
                    <div class="widget-body">
                        <fieldset>
                            <div class="widget-head am-cf">
                                <div class="widget-title am-fl">添加活动</div>
                            </div>
                            <div class="goods-spec-single">
                                <div class="am-form-group">
                                    <label class="am-u-sm-3 am-u-lg-2 am-form-label form-require">标题 </label>
                                    <div class="am-u-sm-9 am-u-md-6 am-u-lg-5 am-u-end">
                                        <input type="text" class="tpl-form-input" name="action[title]" required>
                                        <div class="help-block">
                                            <small>例如：幸运大抽奖</small>
                                        </div>
                                    </div>

                                </div>
                                <div class="am-form-group">
                                    <label class="am-u-sm-3 am-u-lg-2 am-form-label form-require"> 活动时间 </label>
                                    <div class="am-u-sm-9 am-u-md-6 am-u-lg-5 am-u-end">
                                        <!-- 会员等级列表-->
                                        <div class="am-input-group">
                                            <input type="text" class="j-laydate-start am-form-field"
                                                   name="action[start_time]"
                                                   placeholder="开始时间" required>
                                            <span class="am-input-group-label am-input-group-label__center">至</span>
                                            <input type="text" class="j-laydate-end am-form-field"
                                                   name="action[end_time]"
                                                   placeholder="结束时间" required>
                                        </div>
                                        <div class="help-block">
                                            <small>活动的开始时间与截止时间</small>
                                        </div>
                                    </div>
                                </div>
                                <div class="am-form-group">
                                    <label class="am-u-sm-3 am-u-lg-2 am-form-label form-require">参与用户 </label>
                                    <div class="am-u-sm-9 am-u-md-6 am-u-lg-5 am-u-end">
                                            <label class="am-radio-inline">
                                                <input type="radio" name="users" value="0" data-am-ucheck checked>所有用户
                                            </label>
                                            <label class="am-radio-inline">
                                                <input type="radio" name="users" value="1" data-am-ucheck> 部分用户
                                            </label>
                                            <div class="am-form-file am-margin-top-xs">
                                                <button type="button"
                                                        class="j-selectUsers upload-file am-btn am-btn-secondary am-radius">
                                                    <i class="am-icon-cloud-upload"></i> 选择用户
                                                </button>
                                                <div class="widget-users-list uploader-list am-cf">
                                                </div>
                                            </div>

                                        <div class="help-block">
                                            <small>部分用户时必须指定用户</small>
                                        </div>
                                    </div>

                                </div>
                                <div class="am-form-group">
                                    <label class="am-u-sm-3 am-u-lg-2 am-form-label form-require">活动类型 </label>
                                    <div class="am-u-sm-9 am-u-md-6 am-u-lg-5 am-u-end">
                                        <label class="am-radio-inline">
                                            <input type="radio" name="action[integral]" value="0" data-am-ucheck>

                                            无限制
                                        </label>
                                        <label class="am-radio-inline">
                                            <input type="radio" name="action[integral]" value="1" data-am-ucheck checked> 积分抽奖
                                        </label>
                                        <div class="am-input-group am-padding-top">
                                            <span class="am-input-group-label am-input-group-label__left">所需</span>
                                            <input type="number" name="integral" class="am-form-field" min="0" value="0">
                                            <span class="am-input-group-label am-input-group-label__right">积分</span>
                                        </div>
                                        <div class="help-block">
                                            <small>积分抽奖时必须设置</small>
                                        </div>
                                    </div>

                                </div>
                                <div class="am-form-group">
                                    <label class="am-u-sm-3 am-u-lg-2 am-form-label form-require">参与方式 </label>
                                    <div class="am-u-sm-9 am-u-md-6 am-u-lg-5 am-u-end">
                                        <label class="am-radio-inline">
                                            <input type="radio" name="action[way]" value="1" data-am-ucheck checked>每天N次
                                        </label>
                                        <div class="am-input-group am-padding-top">
                                            <span class="am-input-group-label am-input-group-label__left">每人每天可抽</span>
                                            <input type="number" name="day_way" class="am-form-field" min="0" value="0" required>
                                            <span class="am-input-group-label am-input-group-label__right">次</span>
                                        </div>
                                        <label class="am-radio-inline">
                                            <input type="radio" name="action[way]" value="2" data-am-ucheck>每人N次
                                        </label>
                                        <div class="am-input-group am-padding-top">
                                            <span class="am-input-group-label am-input-group-label__left">每人最多可抽</span>
                                            <input type="number" name="count_way" class="am-form-field" min="0" value="0" required>
                                            <span class="am-input-group-label am-input-group-label__right">次</span>
                                        </div>
                                    </div>
                                </div>
                                <div class="am-form-group">
                                    <label class="am-u-sm-3 am-u-lg-2 am-form-label">规则说明 </label>
                                    <div class="am-u-sm-9 am-u-md-6 am-u-lg-5 am-u-end">
                                        <textarea class="am-field-valid" rows="7" placeholder="抽奖规则说明"
                                                  name="action[explain]"></textarea>
                                    </div>
                                </div>

                            <div class="am-form-group">
                                <div class="am-u-sm-10 am-u-sm-push-2 am-margin-top-lg">
                                    <button type="submit" class="j-submit am-btn am-btn-sm am-btn-secondary">提交
                                    </button>
                                </div>
                            </div>
                        </fieldset>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<!-- 用户列表 -->
<script id="tpl-users-list-item" type="text/template">
    {{ each $data }}
    <div class="file-item">
        <a href="{{ $value.avatarUrl }}" title="{{ $value.nickname }}" target="_blank">
            <img src="{{ $value.avatarUrl }}">
        </a>
        <input type="hidden" name="action[users][]" value="{{ $value.user_id }}">
    </div>
    {{ /each }}
</script>

<script src="assets/common/plugins/laydate/laydate.js"></script>
<script src="assets/store/js/select.data.js?v=<?= $version ?>"></script>
<script>
    $(function () {

        // 时间选择器
        laydate.render({
            elem: '.j-laydate-start',
            type: 'datetime'
        });

        // 时间选择器
        laydate.render({
            elem: '.j-laydate-end',
            type: 'datetime'
        });

        // 选择用户
        $('.j-selectUsers').selectData({
            title: '选择用户',
            uri: 'user/lists',
            dataIndex: 'user_id',
            done: function (data) {
                var $html = $(template('tpl-users-list-item', data));
                $('.widget-users-list').html($html);
            }
        });


        /**
         * 表单验证提交
         * @type {*}
         */
        $('#my-form').superForm({
            // 自定义验证
            validation: function () {
                //部分用户
                let users = $('input:radio[name="users"]:checked').val()
                let users_id = $('input:hidden[name="action[users][]"]').val()
                if (users == 1 && users_id == undefined) {
                    layer.msg("部分用户时必须选择可抽奖的用户")
                    return false
                }
                let action_integral = $('input:radio[name="action[integral]"]:checked').val()
                let integral = $('.am-input-group').find('input[type=number][name="integral"]').val()
                if (action_integral > 0 && integral <= 0) {
                    layer.msg("积分抽奖时，积分不能为0")
                    return false
                }
                let action_way = $('input:radio[name="action[way]"]:checked').val()
                let day_way= $('.am-input-group').find('input[type=number][name="day_way"]').val()
                if (action_way == 1 && day_way <= 0) {
                    layer.msg("每天N次，抽奖次数不能为0")
                    return false
                }
                let count_way= $('.am-input-group').find('input[type=number][name="count_way"]').val()
                if (action_way == 2 && count_way <= 0) {
                    layer.msg("每人N次，抽奖次数不能为0")
                    return false
                }
                return true;
            }
        });
    });
</script>
